﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using TuongLaiDangCho.DTO;
using TuongLaiDangCho.DAO;

namespace TuongLaiDangCho
{
    public partial class ucExpenditure : DevExpress.XtraEditors.XtraUserControl
    {
        ChiTieuDTO Expenditure = new ChiTieuDTO();
        public ucExpenditure()
        {
            InitializeComponent();
            Expenditure.NgayThang = frmHome.choiseDate;
            groExpenditure.Text = DataProvider.Today(frmHome.choiseDate) + " " + frmHome.choiseDate.ToShortDateString();
        }

        void LoadExpen()
        {
            grc_Expenditure.DataSource = ChiTieuDAO.LoadExpenditure(Expenditure);
        }

        private void ucExpenditure_Load(object sender, EventArgs e)
        {
            //XtraMessageBox.Show(Expenditure.NgayThang.ToShortDateString());
            LoadExpen();
        }

        private void grv_Expenditure_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            string s_Error = "";
            bool bVali = true;
            if(grv_Expenditure.GetRowCellValue(e.RowHandle,"NoiDung").ToString()==""||grv_Expenditure.GetRowCellValue(e.RowHandle,"SoTien").ToString()=="")
            {
                bVali = false;
                s_Error = "Không thể để trống mục này";
                LoadExpen();
            }

            if (bVali)
            {
                if (e.RowHandle < 0)
                {
                    Expenditure.NoiDung = grv_Expenditure.GetRowCellValue(e.RowHandle, "NoiDung").ToString();
                    Expenditure.SoTien = double.Parse(grv_Expenditure.GetRowCellValue(e.RowHandle, "SoTien").ToString());
                    ChiTieuDAO.InsertExpenditure(Expenditure);
                    LoadExpen();
                }
                else
                {
                    Expenditure.NoiDung = grv_Expenditure.GetRowCellValue(e.RowHandle, "NoiDung").ToString();
                    Expenditure.SoTien = double.Parse(grv_Expenditure.GetRowCellValue(e.RowHandle, "SoTien").ToString());
                    Expenditure.Id = int.Parse(grv_Expenditure.GetRowCellValue(e.RowHandle, "ID").ToString());                    
                    ChiTieuDAO.UpdateExpenditure(Expenditure);
                    //LoadExpen();
                }
            }
            else
            {
                bVali = false;
                XtraMessageBox.Show(s_Error);
            }
        }

        private void grv_Expenditure_InvalidRowException(object sender, DevExpress.XtraGrid.Views.Base.InvalidRowExceptionEventArgs e)
        {
            e.ExceptionMode = DevExpress.XtraEditors.Controls.ExceptionMode.NoAction;
        }

        private void grc_Expenditure_ProcessGridKey(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete && grv_Expenditure.State != DevExpress.XtraGrid.Views.Grid.GridState.Editing)
            {
                if (XtraMessageBox.Show("Are You Sure?", "Quets", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                {
                    Expenditure.Id = int.Parse(grv_Expenditure.GetRowCellValue(grv_Expenditure.FocusedRowHandle, "ID").ToString());
                    ChiTieuDAO.DeleteExpenditure(Expenditure);
                    LoadExpen();
                }
            }
        }       

    }
}
